using UnityEngine;

public class UI_Craft : MonoBehaviour
{
    [SerializeField] private UI_ItemSlotParent itemSlotParent;
    private UI_CraftItemListSlot[] craftItemListSlots;
    private UI_CraftCategoryButton[] craftCategoryButtons;

    private UI_CraftPreview craftPreview;
    private Inventory_Player playerInventory;
    private void Awake()
    {
        UpdateAllSlots();
    }

    //设置工艺制造面板，当玩家和NPC_Smith交互时调用
    public void SetupCraftUI(Inventory_Storage storage)
    {
        craftPreview = GetComponentInChildren<UI_CraftPreview>();
        craftPreview.SetupCraftPreview(storage);

        playerInventory = storage.playerInventory;
        
        playerInventory.OnInventoryChange += UpdateItemSlot;

        //更新玩家物品卡槽
        UpdateItemSlot();

    }
    //获取所有的工艺分类按钮并设置，获取所有能制造的物品列表的卡槽并将其设置为不可见
    public void UpdateAllSlots()
    {
        craftItemListSlots = GetComponentsInChildren<UI_CraftItemListSlot>();
        craftCategoryButtons = GetComponentsInChildren<UI_CraftCategoryButton>();

        foreach (var button in craftCategoryButtons)
            button.SetCraftItemListSlots(craftItemListSlots);

        foreach (var slot in craftItemListSlots)
            slot.gameObject.SetActive(false);
    }

    public void UpdateItemSlot() => itemSlotParent.UpdateSlot(playerInventory.itemList);
}
